package com.hwadee.springboot.service.impl;

import jakarta.mail.MessagingException;
import jakarta.mail.internet.MimeMessage;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.mail.SimpleMailMessage;
import org.springframework.mail.javamail.JavaMailSender;
import org.springframework.mail.javamail.MimeMessageHelper;
import org.springframework.stereotype.Service;


@Service
public class EmailServiceImpl {

    @Autowired
    private JavaMailSender mailSender;

    @Value("${app.mail.from.address}")
    private String fromAddress;
    public void sendHtmlEmail(String to, String subject, String htmlContent) throws MessagingException {
        MimeMessage message = mailSender.createMimeMessage();
        MimeMessageHelper helper = new MimeMessageHelper(message, true, "UTF-8"); // 显式指定字符编码为 UTF-8

        helper.setTo(to);
        helper.setSubject(subject);
        helper.setText(htmlContent, true); // 设置内容为 HTML

        helper.setFrom(fromAddress); // 发件人地址

        mailSender.send(message);
    }

    public void sendSimpleEmail(String to, String subject, String text) {
        SimpleMailMessage message = new SimpleMailMessage();
        message.setTo(to);
        message.setSubject(subject);
        message.setText(text);

        // 显式设置发件人地址
        message.setFrom(fromAddress); // 授权用户的邮箱地址

        mailSender.send(message);
    }
}